package ;
import org.flixel.FlxSprite;
import org.flixel.FlxG;
import org.flixel.FlxObject;
import org.flixel.tweens.misc.VarTween;
import org.flixel.util.FlxAngle;
import org.flixel.util.FlxPoint;
/**
 * ...
 * @author Federico Donnet
 */

 
class PlayableCharacter extends BaseCharacter
{


	public	var joypad:Joystick			=	null;	
	public	var isHurt:Bool				=	false;
	public	var elapsedTime:Float		=	0.0;
	public	var hurtTime:Float			=	3.0;
	public	var playerID:Int			=	0;
	public	var alphaTween:VarTween		=	null;	
	public	var	ResetPosition:FlxPoint	=	null;
	public	var ResetDirection:Int		=	1;
	public	var nPlayer:Int				=	1;
	public	var wallJump:Bool			=	false;
	public	var wallJumpTime:Float		=	0.0;
	
	public  var coins:Int				= 	0;
	
	
	
	public override function update()
	{
		
		if (this.isHurt)
		{
			this.elapsedTime += FlxG.elapsed;
			if (this.elapsedTime >= this.hurtTime)
			{
				this.elapsedTime		=	0.0;
				this.isHurt				=	false;
				this.set_alpha(1);
				if (this.alphaTween != null)
				{
					this.alphaTween.active	=	false;
				}
			}
		}
		
		
		if (!this.wallJump)
		{
				
			//---*El chequeo de inputs de la clase joystick:
			this.joypad.Check_Inputs();
		}
		else
		{
			this.wallJumpTime += FlxG.elapsed;
			
			if (this.wallJumpTime >= 0.25)
			{
				this.wallJump = false;
				this.wallJumpTime = 0.0;
			}
		}
		
		
		//---*Si no se presiona ni derecha ni izquierda, no hay movimiento sobre el eje X:
		if (!this.joypad.Left && !this.joypad.Right)
		{
			this.isMoveXFlag			=	false;
			this.acceleration.x			=	0;
			this.velocity.x				=	0;
				
		}
		//---*Si se presiona Izquierda:
		else if (this.joypad.Left)
		{
			//---*La direccion del movimiento en Eje X es negativa:
			this.dx					=	-1;			
			this.isMoveXFlag = !this.Touch(CharConst.FLAG_LEFT);// si no toca nada a izquierda, !Touch(left)
		}
		
		else if (this.joypad.Right)
		{
			//---*La direccion del movimiento en Eje X es positiva:
			this.dx					=	1;		
			this.isMoveXFlag = !this.Touch(CharConst.FLAG_RIGHT);// si no toca nada a derecha, !Touch(right)
		}
		
		//---* Orientamos el sprite segun la direccion "dx" hacia donde se presiono ultimo, derecha o izquierda
		this.set_facing( ( (dx > 0) ? FlxObject.RIGHT : FlxObject.LEFT ));
		
		//---*?
		
		FSM();
		super.update();	
		
		if (this.platform != null)
		{
			if (this.Touch(CharConst.FLAG_CEIL))
			{
				this.platform.target = null;
				this.platform = null;
				this.isDead = true;
				
			}
		}
		
		if (this.isDead)
		{
			this.Death();
		}
		
		
		if (this.coins >= 50)
		{
			this.coins = 0;
			this.health += 1;
			if (this.health > 2 )
			{
				this.health = 2;
			}
		}
		

	}
	

	public function FSM():Void
	{
		switch(this.state)
		{
			case CharConst.STAND:
			{
				//---*Si hay movimiento lateral y no choca con paredes, A o D:
				if (this.isMoveXFlag && !this.Touch(CharConst.FLAG_WALL))
				{
					//---*Pasamos a correr
					ChangeState(CharConst.RUN);
				}
			
				
				//---*Si se presiona hacia arriba, W:
				if (this.joypad.B)
				{
					//---*Saltamos con (-)vy
					this.Jump(this.gravity * this.JumpVelocity);
					//---*Cambio de estado y animacion
					ChangeState(CharConst.JUMP);
				}
				
				//---*Si se dispara presionando, A:
				if (this.joypad.A)
				{
					GenerateShoot();
					if(this.name!= "ClassicMario" && this.name !="Sonic" && this.name!="Mario")
					ChangeState(CharConst.SHOOT);
				}
				
				
				
				
				
				//---*Si no hay piso entonces cae
				if (!this.Touch(CharConst.FLAG_FLOOR))
				{
					ChangeState(CharConst.JUMP);
				}
				
			}//---*END STAND
			
			case CharConst.RUN:
			{
				//---*Si no se mueve hacia los laterales o choca alguna pared
				if (!this.isMoveXFlag || this.Touch(CharConst.FLAG_WALL) )
				{
					ChangeState(CharConst.STAND);
				}
				
				//---*Si se presiona hacia arriba, W:
				if (this.joypad.B)
				{
					//---*Saltamos con (-)vy
					this.Jump(this.gravity * this.JumpVelocity);
					//---*Cambio de estado y animacion
					ChangeState(CharConst.JUMP);
				}
				
				//---*Si se dispara presionando, A:
				if (this.joypad.A)
				{
					GenerateShoot();
					if(this.name!= "ClassicMario" && this.name !="Sonic" && this.name!="Mario")
					ChangeState(CharConst.SHOOT);
					//---*Detenemos el movimiento lateral
					this.isMoveXFlag = false;
				}
				
				//---*Si no hay piso entonces cae
				if (!this.Touch(CharConst.FLAG_FLOOR))
				{
					ChangeState(CharConst.JUMP);
				}
				
			}//---*END RUN
			
			case CharConst.JUMP:
			{				
				//---*Si se presiona hacia arriba, W:
				if (!this.Touch(CharConst.FLAG_WALL) && !this.wallJump)
				{
					
					if (this.joypad.B && this.nJump < 1)
					{//---*Hacemos el doble salto
						//---*Saltamos con (-)vy
						this.Jump(this.gravity * this.JumpVelocity);
						//---*Cambio de estado y animacion
						ChangeState(CharConst.JUMP, 0, true);
						
					}
				}
				
				//---*Wall Jump
				if (this.Touch(CharConst.FLAG_WALL))
				{
					
					
					if (this.joypad.B )
					{					
						var dirx:Float = 0;
						if (this.Touch(CharConst.FLAG_LEFT))
						{
							//this.facing	= FlxObject.RIGHT;
							this.joypad.Left 	=	false;
							this.joypad.Right 	=	true;
							//this.dx  = 1;
							//this.acceleration.x = 500;
							this.wallJump       = true;
							dirx				= this.x;
							this.x += 10;
							//super.update();
						}
						
						else if (this.Touch(CharConst.FLAG_RIGHT))
						{
							//this.facing	= FlxObject.RIGHT;
							this.joypad.Left 	=	true;
							this.joypad.Right 	=	false;
							//this.dx  = 1;
							//this.acceleration.x = 500;
							this.wallJump     	=	true;
							dirx				= 	this.x + this.width;
							this.x -= 10;
							//super.update();
						}
						
						
						FlxG.play(CharConst.WALL_JUMP_SOUND);
						this.joypad.B = false;
						this.playState.emitterController.PlayEmitter("Dust", dirx, this.y + this.height, this.lastFallVelocity);
						//---*Hacemos el doble salto
						//---*Saltamos con (-)vy
						this.Jump(this.gravity * this.JumpVelocity);
						this.nJump -= 1;
						
						
						//---*Cambio de estado y animacion
						ChangeState(CharConst.JUMP, 0, true);
						
						
					
					}
				}
				
				
				
				
				
				
				
				//---*Si se dispara presionando, A:
				if (this.joypad.A)
				{
					GenerateShoot();
					if (this.name != "ClassicMario" && this.name != "Sonic" && this.name!="Mario")
					{
						if (this.name == "Megaman" || this.name == "ClassicMegaman")
						{
							ChangeState(CharConst.JUMP_SHOOT);
						}
						else 
						{
							ChangeState(CharConst.SHOOT);
						}
					}
				}
				
				//---*Si choca contra el piso, entonces cambio a STAND:
				if (this.Touch(CharConst.FLAG_FLOOR))
				{
					ChangeState(CharConst.STAND);
				}
				
			}//---*END JUMP
			
			case CharConst.SHOOT:
			{
				if (this.finished)
				{
					ChangeState(CharConst.STAND);
				}
				if (this.joypad.Left || this.joypad.Right)
				{
					ChangeState(CharConst.RUN);
				}
				//---*Si se presiona hacia arriba, W:
				if (this.joypad.B && this.nJump < 1)
				{
					//---*Hacemos el doble salto
					//---*Saltamos con (-)vy
					this.Jump(this.gravity * this.JumpVelocity);
					//---*Cambio de estado y animacion					
					ChangeState(CharConst.JUMP);
				}
			}
			
			case CharConst.JUMP_SHOOT:
			{
								
				if (this.joypad.A)
				{
					GenerateShoot();
				}
				
				//---*Si choca contra el piso, entonces cambio a STAND:
				if (this.Touch(CharConst.FLAG_FLOOR))
				{
					ChangeState(CharConst.STAND);
				}
			}
			case CharConst.DIE:
			{
				this.isMoveXFlag = false;
				this.angle += FlxG.elapsed * 450;
				
				if (!this.visible)
				{
					Reset();
				}
			}
		}
	}
	
	public function GenerateShoot():Void 
	{	//despues sobrecargar según el player
		
	}
	
	public function onEnemyCollide(d:Int=0):Void
	{
		if (!this.isHurt)
		{
			this.health -= d;
			if (this.health <= 0)
			{
				this.isDead = true;
				
			}
			else
			{
				this.playState.emitterController.PlayEmitter("HearthParticle", this.x, this.y);
				this.isHurt	=	true;
				if (this.alphaTween != null)
				{
					this.alphaTween.start();
					
				}
			}
		}
	}	
	
	
	
	
	
	public function onTweenEnded():Void
	{
		
	}
	
	public	function Death():Void
	{
		if (this.platform != null)
			{
				this.platform.target = null;
				this.platform = null;
			}
			
		if (this.nPlayer == 1)
		{
			FlxG.camera.target = null;
		}
				
		this.velocity.x = 0;		
		this.acceleration.x = 0;
		this.CollideFlag = 0;
		this.solid = false;
		this.ChangeState(CharConst.DIE);
		this.Jump(800, false);	
		this.isDead = false;
		this.weapon		= CharConst.WEAPON_SIMPLE;
		this.ammo		= 0;
		this.health		=	0;
		
	}
	
	public function Reset():Void
	{
		
	
		this.solid = true;
		this.active = true;
		this.visible = true;
		this.exists = true;
		this.health = 2;
		this.isHurt = true;
		this.alphaTween.start();
		this.dx = this.ResetDirection;
		this.isDead = false;
		this.angle  = 0.0;
		this.x = this.ResetPosition.x;
		this.y = this.ResetPosition.y ;
		this.ChangeState(CharConst.JUMP);
		
		if (this.nPlayer == 1)
		{
			FlxG.camera.target = this;
		}
	}
	
}